using System.Windows.Forms;
using System.Net;
using System;
using Pretorianie.Tytan.Core.Helpers;
using System.Drawing;
using System.Diagnostics;

namespace Pretorianie.Tytan.Forms
{
    public partial class AboutBoxUpdateForm : Form
    {
        private string localVersion;
        private string remoteVersion;
        private WebClient client;
        private string advice;
        private bool showButton;

        /// <summary>
        /// Default constructor.
        /// </summary>
        public AboutBoxUpdateForm()
        {
            InitializeComponent();
        }

        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            SetupUI();
        }

        /// <summary>
        /// Asks remote server for version info and compares go them to give proper advice to the user.
        /// </summary>
        public void SetupUI(string currentVersion, string serverVersionURL, Bitmap logo)
        {
            try
            {
                localVersion = currentVersion;
                advice = SharedStrings.AboutUpdate_DefaultAdvice;
                remoteVersion = SharedStrings.AboutUpdate_Checking;
                lblLocalVersion.Text = localVersion;
                lblRemoteVersion.Text = remoteVersion;
                pictureBox.BackgroundImage = logo;

                client = new WebClient();
                client.DownloadStringCompleted += RemoteVersionCompleted;
                client.DownloadStringAsync(new Uri(serverVersionURL));

            }
            catch
            {
                lblRemoteVersion.Text = SharedStrings.AboutUpdate_UnknownVersion;
            }
        }

        /// <summary>
        /// Refreshes info on the GUI about the version.
        /// </summary>
        protected void SetupUI()
        {
            try
            {
                lblAdvice.Text = advice;
                lblRemoteVersion.Text = remoteVersion;
                bttHomepage.Left = lblAdvice.Left + lblAdvice.Width + 8;
                bttHomepage.Visible = showButton;
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }

        void RemoteVersionCompleted(object sender, DownloadStringCompletedEventArgs e)
        {
            try
            {
                string[] data = e.Result != null
                                    ? e.Result.Split(new char[] {'\r', '\n'}, StringSplitOptions.RemoveEmptyEntries)
                                    : null;
                string version = SharedStrings.AboutUpdate_UnknownVersion;
                string paramName = SharedStrings.AboutUpdate_ParamName;

                if (data != null)
                {
                    foreach (string s in data)
                        if (s.StartsWith(paramName, StringComparison.CurrentCultureIgnoreCase))
                        {
                            version = s.Substring(s.IndexOf(':') + 1).Trim();
                            break;
                        }
                }

                remoteVersion = version;
                if (Visible && InvokeRequired)
                    Invoke(new EventHandler(RemoteVersionReceived));
                else
                    RemoteVersionReceived(this, EventArgs.Empty);
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }

        private void RemoteVersionReceived(object sender, EventArgs e)
        {
            try
            {
                Version c = new Version(localVersion);
                Version r = new Version(remoteVersion);

                if (c > r)
                {
                    advice = SharedStrings.AboutUpdate_VersionTooNew;
                }
                else if (c == r)
                {
                    advice = SharedStrings.AboutUpdate_VersionLatest;
                }
                else
                {
                    advice = SharedStrings.AboutUpdate_VersionUpdate;
                    showButton = true;
                }

                SetupUI();
            }
            catch (Exception ex)
            {
                Trace.WriteLine(ex.Message);
            }
        }

        private void bttHomepage_Click(object sender, EventArgs e)
        {
            CallHelper.OpenBrowser(SharedStrings.About_Releases);
            Close();
        }
    }
}